package com.sap.sap_mangers.service.Impl;

import com.sap.sap_mangers.mapper.DynamicMapper;
import com.sap.sap_mangers.service.DynamicService;
import com.sap.sap_mangers.until.SimpleJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.Resource;

/**
 * @author ZhouKai
 * 2022/8/2 22:37
 */
@Service
public class DynamicServiceImpl implements DynamicService {
    @Autowired
    DynamicMapper dynamicMapper;
    @Resource
    DataSourceTransactionManager transactionManager;

    @Override
    @Transactional
    public SimpleJson changeYear() {
        TransactionStatus txStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try {
            //获取原年份  (注意方法返回的是String,因为sql中字段为varchar)
            int year = Integer.parseInt(dynamicMapper.getYear());
            //年份+1
            dynamicMapper.changeYear(String.valueOf(year + 1));
            //活动次数和星期数归1
            dynamicMapper.changeOrder("1");
            dynamicMapper.changeWeek("1");
        } catch (Exception e) {
            transactionManager.rollback(txStatus);
            e.printStackTrace();
            return SimpleJson.FAILURE;
        }
        return SimpleJson.SUCCESS;
    }

    @Override
    @Transactional
    public SimpleJson changeOrder() {
        TransactionStatus txStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try {
            //活动次数+1
            int order = Integer.parseInt(dynamicMapper.getOrder());
            dynamicMapper.changeOrder(String.valueOf(order + 1));
            //星期数归0
            dynamicMapper.changeWeek("0");
        } catch (Exception e) {
            transactionManager.rollback(txStatus);
            e.printStackTrace();
            return SimpleJson.FAILURE;
        }
        return SimpleJson.SUCCESS;
    }

    @Override
    @Transactional
    public SimpleJson changeWeek() {
        TransactionStatus txStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try {
            int week = Integer.parseInt(dynamicMapper.getWeek());
            dynamicMapper.changeWeek(String.valueOf(week + 1));
        } catch (Exception e) {
            transactionManager.rollback(txStatus);
            e.printStackTrace();
            return SimpleJson.FAILURE;
        }
        return SimpleJson.SUCCESS;
    }

    @Override
    public SimpleJson reduceWeek() {
        TransactionStatus txStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try {
            int week = Integer.parseInt(dynamicMapper.getWeek());
            if (week <= 1) {
                return new SimpleJson(1, "无法继续减小周数");
            }
            dynamicMapper.changeWeek(String.valueOf(week - 1));
        } catch (Exception e) {
            transactionManager.rollback(txStatus);
            e.printStackTrace();
            return SimpleJson.FAILURE;
        }
        return SimpleJson.SUCCESS;
    }


    @Override
    public String getYear() {
        return dynamicMapper.getYear();
    }

    @Override
    public String getOrder() {
        return dynamicMapper.getOrder();
    }

    @Override
    public String getWeek() {
        return dynamicMapper.getWeek();
    }

    @Override
    public Integer getYearForInt() {
        return Integer.parseInt(dynamicMapper.getYear());
    }

    @Override
    public Integer getOrderForInt() {
        return Integer.parseInt(dynamicMapper.getOrder());
    }

    @Override
    public Integer getWeekForInt() {
        return Integer.parseInt(dynamicMapper.getWeek());
    }

    @Override
    public String getViewYear() {
        return dynamicMapper.getViewYear();
    }

    @Override
    public String getViewOrder() {
        return dynamicMapper.getViewOrder();
    }

    @Override
    public Integer getHighestOnlinePeople() {
        return Integer.valueOf(dynamicMapper.getHighestOnlinePeople());
    }

    @Override
    public void setHighestOnlinePeople(String people) {
        dynamicMapper.setHighestOnlinePeople(people);
    }
}
